﻿
CREATE PROCEDURE [dbo].[proc_ysh_DelOrder]
	(
		@OrderId uniqueidentifier
	)
AS

Begin Tran
	Declare @Id int

	Select Top 1 @Id=Id From Project Where OrderId=@OrderId And Year(CreateDate)=2006 And Month(CreateDate)=1
	If @Id > 0	--存在项目分析数据
	Begin
		RollBack Tran
		Return -1
	End

	Declare @CustomerId uniqueidentifier,@CreateDate datetime
	Select @CustomerId=CusId,@CreateDate=CreateDate From [order] Where OId=@OrderId

	Delete From Order_Item Where OId=@OrderId And Year(TradeDate)=2006 And Month(TradeDate)=1
	Delete From [order] Where OId=@OrderId And Year(CreateDate)=2006 And Month(CreateDate)=1
	Delete From Order_PaymentRecord Where OId=@OrderId And Year(CreateDate)=2006 And Month(CreateDate)=1
	Delete From Project Where OrderId=@OrderId And Year(CreateDate)=2006 And Month(CreateDate)=1

	if @CustomerId is Not null
		Delete From Customer Where CustomerId=@CustomerId And Year(CreateDate)=2006 And Month(CreateDate)=1

   If @@error <> 0  --发生错误
		Begin
			RollBack Tran
			Return -2
		End
    Else
		Begin
			Commit Tran
			Return    --执行成功
End